package com.ruoyi.patrol.service.impl;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.gson.Gson;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.patrol.mapper.PtMonitorMapper;
import com.ruoyi.patrol.service.IPtMonitorService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 巡查线路Service业务层处理
 * 
 * @author lkm
 * @date 2022-11-07
 */
@Service
public class PtMonitorServiceImpl implements IPtMonitorService {
    private final PtMonitorMapper ptMonitorMapper;

    public PtMonitorServiceImpl(PtMonitorMapper ptMonitorMapper) {
        this.ptMonitorMapper = ptMonitorMapper;
    }

    @Override
    public void update() {
        String s = HttpUtils.sendPost("https://open.ys7.com/api/lapp/token/get?appKey=adffdf9bb3ab4a8bb07c9fce3a75127b&appSecret=e452ee895a43576d3dbc8d1ec746b5fd", "");
        Gson gson = new Gson();
//        Type type = new TypeToken<Map<String, String>>() {}.getType();
        Demo demo = gson.fromJson(s, Demo.class);
        String accessToken = demo.getData().getAccessToken();
        List<Map<String, Object>> query = ptMonitorMapper.query();
        for (Map<String, Object> map : query) {
            map.put("token", accessToken);
        }
        ptMonitorMapper.update(query);

    }
    class Demo {
        private String msg;
        private String code;
        private Data data;

        @JsonProperty("msg")
        public String getMsg() { return msg; }
        @JsonProperty("msg")
        public void setMsg(String value) { this.msg = value; }

        @JsonProperty("code")
        public String getCode() { return code; }
        @JsonProperty("code")
        public void setCode(String value) { this.code = value; }

        @JsonProperty("data")
        public Data getData() { return data; }
        @JsonProperty("data")
        public void setData(Data value) { this.data = value; }
        class Data {
            private String accessToken;
            private long expireTime;

            @JsonProperty("accessToken")
            public String getAccessToken() { return accessToken; }
            @JsonProperty("accessToken")
            public void setAccessToken(String value) { this.accessToken = value; }

            @JsonProperty("expireTime")
            public long getExpireTime() { return expireTime; }
            @JsonProperty("expireTime")
            public void setExpireTime(long value) { this.expireTime = value; }
        }
    }
}


